import React from 'react';

import EntryAddStyle from "../assets/css/EntryAdd.module.css";
import {Button, List, InputItem, TextareaItem, ImagePicker,Toast,Picker} from "antd-mobile";
import {UploadImage,SaveLeaveEnterInfo,GetEnterVarietyList} from "../http/Request";


let filesData = [];
let filesObj = {};



export default class EntryAdd extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            files: filesData,
            imageUrl: '',
            ageValue:'',
            weightValue:'',
            LivestockType:'',
            originPlace:'',
            remarks:'',
            time:new Date().getTime(),
            leaveType:'1001',
            TypeList:[],
            sValue:'',
            enterVarietyName:'',
        };


    }

    showToast() {
        Toast.info('添加成功', 1);
    };

    onChangeAddImg = (files, type, index) => {
        let formData = new FormData();
        formData.append('file', files[0]['file']);
        UploadImage(formData).then(resolve => {
            filesObj['url'] = resolve.body;
            this.state.files.push(filesObj);
            this.setState({
                files:this.state.files,
                imageUrl: resolve.body
            });
            console.log(this.state.imageUrl,this.state.files);
        }).catch(err => {

        });
    };

  
    //获取年龄
    onChangeAge = (value) => {
        this.setState({
            ageValue:value,
        });

    }
    //获取体重
    onChangeWeight = (value)  => {
        this.setState({
            weightValue:value,
        });
    }
    //获取种类列表
    GetLivestockType(){
        let body = {
            varietyType:'9001'
        };
        GetEnterVarietyList(body).then(resolve => {
            let Resolvearr = resolve.body
            let DataArr = []
            Resolvearr.forEach(element => {
               console.log(element)
                let CurrentObj = {};
                CurrentObj['value'] = element.varietyId;
                CurrentObj['label'] = element.varietyName;
                DataArr.push(CurrentObj)
            });
            console.log(DataArr);

            this.setState({
                TypeList:DataArr
            })

        }).catch(err => {

        })
    }
    // 获取来源地
    onChangeOriginPlace = (value) => {
        this.setState({
            originPlace:value
        })
    }
    //获取我的备注
    onChangeRemarks = (value) => {
        this.setState({
            remarks:value
        })
    }
    /**
     * 提交
     * */
    submitData() {
        // let  parseInt()
        if (this.state.ageValue.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === '') {
            Toast.offline('请输入入栏年龄', 1);
            return;
        }
        if(this.state.weightValue.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入体重', 1);
            return;
        }
        let VarketyName = document.getElementsByClassName('am-list-extra')[0].innerText;
        if(VarketyName === '请选择'){
            Toast.offline('请选择种类', 1);
            return;
        }
        if(this.state.originPlace.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入来源地', 1);
            return;
        }

        let body = {};
        body['enterAge'] = parseInt(this.state.ageValue);
        body['enterWeight'] = parseInt(this.state.weightValue);
        body['enterTime'] = parseInt(this.state.time);
        body['enterPrice'] = '';
        body['enterSourceAddress'] = this.state.originPlace;
        body['enterImages'] = this.state.imageUrl;
        body['leaveType'] = this.state.leaveType;
        body['enterVarietyId'] = this.state.sValue[0];
        body['enterVarietyName'] = document.getElementsByClassName('am-list-extra')[0].innerText;
        body['leaveAge'] = '';
        body['leaveWeight'] = '';
        body['leavePrice'] = '';
        body['leaveSourceAddress'] = '';
        body['leaveImages'] = '';
        SaveLeaveEnterInfo([body]).then(resolve => {
            console.log(resolve);
            setTimeout(this.back,1500)
            this.showToast();

        }).catch(err => {

        })
    }

    //生命周期
    componentDidMount() {
        this.GetLivestockType()
    }
    back(){
        window.history.back(-1)
    }
    // handlePickerChange = vs => {
    //     this.vs = vs;
    // };

    //packerChange
    packerChange = (v,) => {
        console.log(v,"v");
        this.setState({ sValue: v })
    }


    render() {
        const {files} = this.state;

        // const { getFieldProps } = this.props.form;
        return (
                <div className={'main'}>
                    <div className={EntryAddStyle.topPart}>

                    </div>
                    <div className={EntryAddStyle.buttonBox}>
                        <Button onClick={this.submitData.bind(this)} type="ghost" inline size="small">完成</Button>
                    </div>
                    <div className={EntryAddStyle.formBox}>
                        <form action="">
                            <List style={{padding: '0px'}}>
                                <InputItem
                                    type={"number"}
                                    onChange={this.onChangeAge}
                                    value={this.state.ageValue}
                                >
                                    入栏年龄
                                </InputItem>
                                <InputItem
                                    type={"number"}
                                    onChange={this.onChangeWeight}
                                    value={this.state.weightValue}
                                >
                                    入栏体重
                                </InputItem>
                                {/*<InputItem>*/}
                                {/*    入栏价格*/}
                                {/*</InputItem>*/}
                          

                                <Picker data={this.state.TypeList}
                                        cols={1}
                                        value={this.state.sValue}
                                        onChange={v => this.packerChange(v)}
                                        onOk={v => this.setState({ sValue: v })}
                                >
                                    <List.Item arrow="horizontal">种类</List.Item>
                                </Picker>
                                <InputItem
                                    type={"text"}
                                    onChange={this.onChangeOriginPlace}
                                    value={this.state.originPlace}
                                >
                                    来源地
                                </InputItem>
                                <TextareaItem
                                    defaultValue={'填写备注'}
                                    style={{height: '100px',border:'1px solid #fefefe',padding:'5px'}}
                                    title="备注"
                                    onChange={this.onChangeRemarks}
                                    value={this.state.remarks}
                                />
                                <ImagePicker
                                        length="1"
                                        disableDelete={true}
                                        selectable={files.length < 1}
                                        files={files}
                                        onChange={this.onChangeAddImg}
                                />
                            </List>
                        </form>
                    </div>
                </div>
        )
    }
}
